package com.zyl.pts.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyl.pts.config.enums.CodeAndMessageEnum;
import com.zyl.pts.domain.bean.QuarantineRegistration;
import com.zyl.pts.domain.dto.QRDTO.QRUpdateOrAddDTO;
import com.zyl.pts.mapper.QRMapper;
import com.zyl.pts.service.QRService;
import com.zyl.pts.utils.CUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @version v1.0
 * @ProjectName: product_source
 * @Package: com.zyl.pts.service.serviceImpl
 * @ClassName: QuarantineRegistrationServiceImpl
 * @Description TODO()
 * @Author 翟玉龙
 * @Date 2024/11/19-14:13
 */
@Service
public class QRServiceImpl implements QRService {

    private final QRMapper qrMapper;

    public QRServiceImpl(QRMapper qrMapper) {
        this.qrMapper = qrMapper;
    }

    /**
     * 查询隔离登记表
     * @param bQualified 是否合格
     * @param grMechanism 隔离机构
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return {@link PageInfo }<{@link QuarantineRegistration }>
     */
    @Override
    public PageInfo<QuarantineRegistration> queryQuarantineRegistration(String bQualified, String grMechanism, Integer pageNum, Integer pageSize) {
        Page<QuarantineRegistration> page = PageHelper.startPage(pageNum, pageSize);
        List<QuarantineRegistration> qrList = qrMapper.queryQuarantineRegistration(bQualified, grMechanism);
        PageInfo<QuarantineRegistration> pageInfo = new PageInfo<>(qrList);
        page.close();
        return pageInfo;
    }

    /**
     * 更新或者新增检疫记录
     * @param qrUpdateOrAdd qrUpdateOrAdd
     * @return {@link CodeAndMessageEnum }
     */
    @Transactional
    @Override
    public CodeAndMessageEnum addOrUpdate(QRUpdateOrAddDTO qrUpdateOrAdd) {
        CUtil.logInfo(qrUpdateOrAdd.toString());
        // 判断是否传入了id值
        if (qrUpdateOrAdd.getGrId() != null) {
            return qrMapper.updateQuarantineRegistration(qrUpdateOrAdd) == 1
                    ? CodeAndMessageEnum.UPDATE_SUCCESS : CodeAndMessageEnum.ERROR_UPDATE;
        }
        return qrMapper.addQuarantineRegistration(qrUpdateOrAdd) == 1
                ? CodeAndMessageEnum.INSERT_SUCCESS : CodeAndMessageEnum.ERROR_INSERT;
    }

    /**
     * 根据检疫id删除记录
     * @param id 检疫id
     * @return {@link CodeAndMessageEnum }
     */
    @Transactional
    @Override
    public CodeAndMessageEnum deleteById(Integer id) {
        int status = qrMapper.deleteById(id);
        if(status == 1) {
            return CodeAndMessageEnum.DELETE_SUCCESS;
        }
        return CodeAndMessageEnum.ERROR_NOT_EXIST;
    }

    /**
     * 根据id列表批量删除检疫记录
     * @param ids id列表
     * @return {@link CodeAndMessageEnum }
     */
    @Transactional
    @Override
    public CodeAndMessageEnum deleteByIdAll(List<Integer> ids) {
        return qrMapper.deleteByIds(ids) ?
                CodeAndMessageEnum.DELETE_MANY_SUCCESS : CodeAndMessageEnum.ERROR_NODATA;
    }
}
